package com.ruoyi.web.controller.approve;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.ExaExpense;
import com.ruoyi.domain.ExaLeave;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.IExaExpenseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 报销业务Controller
 *
 * @author ruoyi
 * @date 2020-07-01
 */
@Controller
@RequestMapping("/ruoyi/expenseVerify")
public class ExaExpenseDeptVerifyController extends BaseController
{
    private String prefix = "ruoyi/expense";

    @Autowired
    private IExaExpenseService exaExpenseService;

    @RequiresPermissions("ruoyi:expenseVerify:view")
    @GetMapping()
    public String expense()
    {
        return prefix + "/expenseVerify";
    }

    /**
     * 查询报销业务列表
     */
    @RequiresPermissions("ruoyi:expenseVerify:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ExaExpense exaExpenses)
    {

        List<ExaExpense> list = exaExpenseService.selectExaExpenseList(exaExpenses);
        list.removeIf(exaExpense -> exaExpense.geteType()==1);
        startPage();

        return getDataTable(list);
    }

    /**
     * 导出报销业务列表
     */
    @RequiresPermissions("ruoyi:expenseVerify:export")
    @Log(title = "报销业务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ExaExpense exaExpense)
    {
        List<ExaExpense> list = exaExpenseService.selectExaExpenseList(exaExpense);
        ExcelUtil<ExaExpense> util = new ExcelUtil<ExaExpense>(ExaExpense.class);
        return util.exportExcel(list, "expense");
    }

    /**
     * 新增报销业务
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存报销业务
     */
    @RequiresPermissions("ruoyi:expenseVerify:add")
    @Log(title = "报销业务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave( ExaExpense exaExpense)
    {
        exaExpense.seteDepa(ShiroUtils.getdept());
        exaExpense.setePicture(ShiroUtils.getavatar());
        exaExpense.seteCreator(ShiroUtils.getLoginName());
        exaExpense.seteTheHeir(ShiroUtils.getLoginName());
        return toAjax(exaExpenseService.insertExaExpense(exaExpense));
    }

    /**
     * 驳回【请假】
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {

        exaExpenseService.editExaStatus(id);
        ExaExpense exaExpense = exaExpenseService.selectExaExpenseById(id);
        mmap.put("exaExpense", exaExpense);
        return prefix + "/Verifyedit";
    }

    /**
     * 驳回保存【请假】
     */
    @RequiresPermissions("ruoyi:expenseVerify:edit")
    @Log(title = "【请假】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ExaExpense exaLeave)
    {
        return toAjax(exaExpenseService.updateExaExpense(exaLeave));
    }
    /**
     * 删除报销业务
     */
    @RequiresPermissions("ruoyi:expenseVerify:remove")
    @Log(title = "报销业务", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(exaExpenseService.deleteExaExpenseByIds(ids));
    }

    /**
     * 提交申请
     */
    @Log(title = "报销业务", businessType = BusinessType.UPDATE)
    @PostMapping( "/submitApply")
    @ResponseBody
    public AjaxResult submitApply(Long id) {
        int leave = exaExpenseService.submitExaStatus(id);
        String applyUserId = ShiroUtils.getLoginName();

        return success();
    }
    /**
     * 撤销申请
     */
    @Log(title = "报销业务", businessType = BusinessType.UPDATE)
    @PostMapping( "/revocation")
    @ResponseBody
    public AjaxResult revocation(Long id) {
        int leave = exaExpenseService.revoExaStatus(id);
        String applyUserId = ShiroUtils.getLoginName();

        return success();
    }
    /**
     * 结束
     */
    @Log(title = "报销业务", businessType = BusinessType.UPDATE)
    @PostMapping( "/reports")
    @ResponseBody
    public AjaxResult reports(Long id) {
        int leave = exaExpenseService.repExaStatus(id);
        String applyUserId = ShiroUtils.getLoginName();

        return success();
    }

    /**
     * 通过申请
     */
    @Log(title = "请假业务", businessType = BusinessType.UPDATE)
    @PostMapping( "/consent")
    @ResponseBody
    public AjaxResult consent(Long id) {
        int leave = exaExpenseService.conExaStatus(id);
        return success();
    }

    /**
     * 下载
     */
    @GetMapping("downloadFile")
    public void resourceDownload(@PathVariable("eRemark") String eRemark, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // 本地资源路径
        String localPath = Global.getProfile();
        // 数据库资源地址
        String downloadPath = localPath + StringUtils.substringAfter(eRemark, Constants.RESOURCE_PREFIX);
        // 下载名称
        String downloadNames = StringUtils.substringAfterLast(downloadPath, "/");
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition",
                "attachment;pFileName=" + FileUtils.setFileDownloadHeader(request, downloadNames));
        FileUtils.writeBytes(downloadPath, response.getOutputStream());
    }

}
